
import com.cec.mediatR.handler.IRequestHandler;
import com.cec.mediatR.service.IMediator;
import org.springframework.scheduling.annotation.Async;

import java.util.ArrayList;
import java.util.List;

public class DemoAService  implements IDemoAService, IRequestHandler<RequestAService, String> {
    private final IMediator mediator;

    public DemoAService(IMediator mediator) {
        this.mediator = mediator;
    }

    @Override
    public String handle(RequestAService request) {

        return this.helloWithB(request.getName());
    }

    @Override
    public String hello() {
        return this.mediator.send(new RequestBService());
    }

    /**
     * @Async注解可以用在方法上,也可以用在类上,用在类上,对类里面所有方法起作用,
     * 一定要其他的类调用这个方法，如果在同类中调用，是不生效的。
     */
    @Async
    @Override
    public void helloAsync() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.mediator.send(new RequestBService());
    }

    @Override
    public List<People> helloC() {
        RequestCService requestCService=new RequestCService();
        People people1=new People();
        people1.setId(1);
        people1.setName("张三1");
        Book book1=new Book();
        book1.setName("书1");
        book1.setPageNum(100);
        Book book2=new Book();
        book2.setName("书2");
        book2.setPageNum(1000);

        List<Book> books1=new ArrayList<>();
        books1.add(book1);
        books1.add(book2);
        people1.setBooks(books1);

        List<People> peoples=new ArrayList<>();
        peoples.add(people1);

        requestCService.setPeoples(peoples);
        return this.mediator.send(requestCService);
    }

    @Override
    public String helloWithB(String name) {
        return name;
    }
}
